#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;

void MyLine( Mat img, Point start, Point end, int type = 1 )
{
  	int thickness = 1;
  	int lineType = 8;
  	int r = 0;
  	int g = 0;
	int b = 0;
	switch(type)
	{
		case 1: r = 255; break;
		case 2: g = 255; break;
		case 3: b = 255; break;
	}  
  	line( img, start, end,  Scalar( r, g, b ), thickness, lineType );
}

Mat ProfilIntensity(Mat image, int row)
{
//	CV_Assert(image.depth() != sizeof(uchar));
	Mat tmp (image.rows+20,image.cols,CV_8UC3,Scalar(0,0,0));
	int cols 	 = image.cols;
	int rows 	 = image.rows; 
	int channels = image.channels();
	int x,y;
	x = row;
	Point start,end;
	switch(channels) 
	{
		case 1:
		{
			for(int i = 0; i < cols; i++)
			{
				start.x = i;
				end.x = i+1;
				start.y = 255 - image.at<uchar>(x,i);
				end.y = 255 - image.at<uchar>(x,i+1);

				//draw line 
				MyLine(tmp,start,end);
				
//				Scalar intensity = image.at<uchar>(x,i);
			}
			break;
		}
		case 3:
		{
			Vec3b itensity;// = img.at<Vec3b>(x, x);
			Vec3b intensity1;
			uchar blue;// = intensity.val[0];
			uchar green;// = intensity.val[1];
			uchar red;// = intensity.val[2];	

			for(int i = 0; i < cols; i++)
			{
				Vec3b intensity  = image.at<Vec3b>(x, i);
				intensity1 = image.at<Vec3b>(x,i+1);

				blue = intensity.val[0];
				green = intensity.val[1];
				red = intensity.val[2];
				
				//line b				
				start.x = i;
				end.x = i+1;
				start.y = 255 - intensity.val[0];
				end.y = 255 - intensity1.val[0];
				MyLine(tmp,start,end,3);
				//line g				
				start.x = i;
				end.x = i+1;
				start.y = 255 - intensity.val[1];
				end.y = 255 - intensity1.val[1];
				MyLine(tmp,start,end,2);
				//line r				
				start.x = i;
				end.x = i+1;
				start.y = 255 - intensity.val[2];
				end.y = 255 - intensity1.val[2];
				MyLine(tmp,start,end,1);

//				tmp.at<uchar>(Point(x,i)) = 255 ;				
			}
			break;
		}
	}
	return tmp;
}

void showImage(Mat img) 
{
	int cols = img.cols;
	printf("%d",cols);
	imshow("Image ",img);
}
int main( int argc, char** argv )
{
	Mat image;
  	Mat image2;
	int weight,height;

  	image = imread( argv[1], 1 );	
  	if( argc != 2 || !image.data )
    {
      printf( "No image data \n" );
      return -1;
    }
  		
	
	//utilise image gray
//	cvtColor(image, image,CV_BGR2GRAY);

	image2 = ProfilIntensity(image,205);
	imshow("Profil intensite",image);
	imshow("aaa",image2);	
  	
	waitKey(0);

  	return 0;
}
